import Vue from 'vue'
import VueRouter from 'vue-router'
// import Activity from '../views/Activity.vue'
import Home from '../views/Home.vue'
// import Share from '../views/Share.vue'
Vue.use(VueRouter)

const routes = [
    {path: "/", redirect: "/login"},
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/Login.vue')
    },
    {
        path: '/find',
        name: 'find',
        component: () => import('@/views/FindPassword.vue')
    },
    {
        path: '/register',
        name: 'register',
        meta: {title: '注册', icon: 'index', affix: true, noCache: true},
        component: () => import('@/views/Register.vue')
    },
    {
        path: '/home',
        name: 'home',
        component: Home,
        children: [
            {
                path: 'usermannage',
                name: 'usermannage',
                component: () => import('@/views/UserMannage.vue')
            },
            {
                path: 'comment',
                name: 'comment',
                component: () => import('@/views/Comment.vue')
            },
            {
                path: 'consult',
                name: 'consult',
                component: () => import('@/views/Consult.vue')
            },
            {
                path: 'activity',
                name: 'activity',
                component: () => import('@/views/ActivityMannage.vue')
            },
            {
                path: 'share',
                name: 'share',
                component: () => import('@/views/ShareMannage.vue')
            },
            {
                path: 'mannager',
                name: 'mannager',
                component: () => import('@/views/Mannager.vue')
            },
            {
                path: 'second',
                name: 'second',
                component: () => import('@/views/SecondMannage.vue')
            },
        ]
    },
    {
        path: '/404',
        name: 'NotFound',
        component: () => import('@/views/features/404.vue')
    },
    // 所有未定义路由，全部重定向到404页
    {
        path: '*',
        redirect: '/404'
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach((to, from, next) => {
    localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称
    const user = localStorage.getItem("user")
    console.log(user)
    console.log(to.name)
    if (to.fullPath === '/login') {
        next()
    } else if (user) {
        next()
    } else {
        next('/login')
    }
    // 其他的
})
export default router